﻿using Knight.Web.Infrastructure.Menus;
using Knight.Web.Infrastructure.Mvc;

using Knight.Common;
using Knight.Models.Entity;
using Knight.Models.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Areas.Manage.Controllers.Filters;
using Knlght.Domain;

namespace WebApp.Areas.Manage.Controllers
{
    public class RoleInfoController : BaseAdminController
    {
        [MallAdminMenu("System_RoleInfo")]
        [Permission("System_RoleInfo")]
        public ActionResult Index()
        {
            string _path = DConfig.MALL_ADMIN_MENU;
            string fullPath = Server.MapPath(_path);
            //从指定的数据路径中加载菜单
            var sr = new System.IO.StreamReader(fullPath);
            var serializer = new Newtonsoft.Json.JsonSerializer();
            var list = serializer.Deserialize(sr, typeof(List<MenuItem>)) as List<MenuItem>;
            ViewBag.MenuDatas = list;
            return View();
        }

        #region AngularJs
        [HttpPost]
        public JsonResult Paging(PagingParameter parameter)
        {
            var query = new QueryRequest<RoleInfo>();
            if (parameter.Query.CreateCriterionByString(nameof(RoleInfo.Name)) != null)
            {
                var name = parameter.Query.CreateCriterionByString(nameof(RoleInfo.Name)).Value.ToString();
                query.Add(o => o.Name.Contains(name));
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(RoleInfo.IsEnabled)) != null)
            {
                var isEnabled = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(RoleInfo.IsEnabled)).Value);
                query.Add(o => o.IsEnabled == isEnabled);
            }
            var service = getDefaultService();
            query.AddOrder(nameof(RoleInfo.AddTime), false);
            query.AddOrder(nameof(RoleInfo.Name), true);
            var pagingRequest = new PagingRequest<RoleInfo>
            {
                PageIndex = parameter.PageIndex,
                PageSize = parameter.PageSize,
                Query = query
            };
            var rsp = service.Paging(pagingRequest);
            return EJPagingJson(rsp);
        }
        [HttpPost]
        public JsonResult Add(RoleInfo model)
        {
            var service = getDefaultService();
            var rsp = service.Add(model);
            return rsp.IsSuccess ? EJActionJson(true, model.Id.ToString()) : EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Update(RoleInfo model)
        {
            var service = getDefaultService();
            var rsp = service.Update(model);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Updates(int[] ids, string property, object value)
        {

            if (property.ToLower() == nameof(RoleInfo.IsEnabled).ToLower())
            {
                var service = getDefaultService();
                var rsp = service.UpdateToList<RoleInfo>(o => ids.Contains(o.Id), i => new RoleInfo { IsEnabled = (bool)value });
                return EJActionJson(rsp);
            }
            else
            {
                return EJActionJson(false, "该属性修改方法没有实现！");
            }
        }
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var service = getDefaultService();
            var rsp = service.GetModel<RoleInfo>(o => o.Id == id);
            if (rsp.IsSuccess)
            {
                var result = service.IsExist<Administrators>(o => o.OwmRoleId == rsp.Model.Id).IsSuccess;
                if (result)
                {
                    return EJActionJson(false, "无法删除原因:已在其它地方有引用");
                }
                else
                {
                    var rsp2 = service.Remove<RoleInfo>(o => o.Id == id);
                    return EJActionJson(rsp2);
                }
            }
            else
            {
                return EJActionJson(false, "无法删除原因:找不到对应信息");
            }         
        }
        [HttpPost]
        public JsonResult Removes(int[] ids)
        {
            var service = getDefaultService();
            var query = new QueryRequest<RoleInfo>();
            query.Add(o => ids.Contains(o.Id));
            var rsp = service.GetList<RoleInfo>(query);
            if (rsp.IsSuccess)
            {
                var result = getDefaultService().IsExist<Administrators>(o => ids.Contains(o.OwmRoleId)).IsSuccess;
                if (result)
                {
                    return EJActionJson(false, "无法删除原因:已在其它地方有引用");
                }
                else
                {
                    var rsp2 = service.Remove<RoleInfo>(o => ids.Contains(o.Id));
                    return EJActionJson(rsp2);
                }
            }
            else
            {
                return EJActionJson(false, "无法删除原因:找不到对应信息");
            }       
        }
        #endregion
    }
}